package fr.mairie.parc.page.commande;

import javax.annotation.Resource;

import org.apache.click.control.Form;
import org.apache.click.control.ImageSubmit;
import org.apache.click.util.Bindable;
import org.springframework.context.ApplicationContext;

import fr.mairie.parc.context.AppContext;
import fr.mairie.parc.control.commande.CommandeFields;
import fr.mairie.parc.domain.Commande;
import fr.mairie.parc.page.BorderPage;
import fr.mairie.parc.service.CommandeManager;

public class ViewCommandePage extends BorderPage {

	private static final long serialVersionUID = 1L;
	@Resource protected CommandeManager commandeManager;
	@Bindable protected Form form = new Form();
	@Bindable protected Integer idCommande;
	protected transient Commande commande;


	protected CommandeFields fields = new CommandeFields(true);

	public ViewCommandePage() {
		fields.setReadonly(true);
		fields.addAll(form);
		form.add(new ImageSubmit("Cancel", "/assets/images/32x32/cancel.png",this, "onCancelClicked"));
	}

	@Override
	public void onInit() {
		super.onInit();
		ApplicationContext ctx = AppContext.getApplicationContext();
    	commandeManager = (CommandeManager) ctx.getBean("commandeManager");
	}

	@Override
	public void onRender() {
		if (idCommande < 0) {
			msg = "Error, invalid id:" + idCommande + " for commande";
			setRedirect("error.htm");
			return;
		}
		if (commande == null) {
			commande =  commandeManager.getCommandeById(idCommande);
		}
		if (commande == null) {
			msg = "Error, commande not found for id:" + idCommande;
			setRedirect("error.htm");
			return;
		}
		fields.setFieldsValues(commande);
		super.onRender();
	}

	public boolean onCancelClicked() {
        setRedirect(ListCommandePage.class);
        return false;
    }
}
